#include "shtc3.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_GPIO.h"


#define SHTC3_DEFAULT_ADDR (0x70 << 1)
#define SHTC3_READID 0xEFC8    /**< Read Out of ID Register */
#define SHTC3_SOFTRESET 0x805D /**< Soft Reset */
#define SHTC3_WAKEUP 0x3517    /**< Wakeup mode */
#define SHTC3_NOR_READ_TEMP_FIR_DIS_CMD 0x7866
#define SHTC3_SLEEP_CMD 0xB098 // ʹоƬ������������

 
u8 TEMP_DATA_Cont;
u8 TEMP_DATA[10];
u8 HUMI_DATA[10];

u8 tempt_dath;
u8 tempt_datl;
u8 humi_dataa;
 

sbit SCL = P1 ^ 2;
sbit SDA = P1 ^ 1;
// iic--------------------------------------------------------------------------
void SHTC3_IO_config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = GPIO_Pin_1 | GPIO_Pin_2;
    GPIO_InitStructure.Mode = GPIO_PullUp;
    GPIO_Inilize(GPIO_P1, &GPIO_InitStructure);
}

void SHTC3_IIC_Start(void)
{
    SDA = 1;     // 拉高数据线
    SCL = 1;     // 拉高时钟线
    delay_us(5); // 延时
    SDA = 0;     // 产生下降沿
    delay_us(5); // 延时
    SCL = 0;     // 拉低时钟线
}
void SHTC3_IIC_Stop()
{
    SDA = 0;      
    SCL = 1;      
    delay_us(5);  
    SDA = 1;      
    delay_us(5);  
}
void STHC3_IIC_SendACK(bit ack)
{
    SDA = ack;    
    SCL = 1;      
    delay_us(5);  
    SCL = 0;      
    delay_us(5);  
}
unsigned char SHTC3_IIC_RecvACK()
{
    char i = 0;
    SCL = 1;      
    delay_us(5);  
    CY = SDA;     
    SCL = 0;      
    delay_us(5);  
    return i;
}
void SHTC3_IIC_SendByte(unsigned char dat)
{
    unsigned char i;
    for (i = 0; i < 8; i++)  
    {
        dat <<= 1;    
        SDA = CY;     
        SCL = 1;     
        delay_us(5);  
        SCL = 0;     
        delay_us(5);  
    }
    SHTC3_IIC_RecvACK();
}
unsigned char IIC_RecvByte(u8 ack)
{

    unsigned char i;
    unsigned char dat = 0;

    SDA = 1;                 
    for (i = 0; i < 8; i++) 
    {
        dat <<= 1;
        SCL = 1;      
        delay_us(5);  
        dat |= SDA;   
        SCL = 0;      
        delay_us(5);  
    }
    STHC3_IIC_SendACK(ack);
    return dat;
}
char SHTC3_iic_test(void)
{
    SHTC3_IIC_Start;
    SHTC3_IIC_SendByte(0x70 << 1);
    return CY;
    SHTC3_IIC_Stop();
}

// shtc3--------------------------------------------------------------------------
unsigned char SHTC3_CRC_CHECK(unsigned short dat, unsigned char crc_data)
{
    unsigned char i, t, temp;
    unsigned char crc_byte;

    crc_byte = 0xFF;
    temp = (dat >> 8) & 0xFF;

    for (t = 0; t < 2; t++)
    {
        crc_byte ^= temp;
        for (i = 0; i < 8; i++)
        {
            if (crc_byte & 0x80)
            {
                crc_byte <<= 1;
                crc_byte ^= 0x31;
            }
            else
            {
                crc_byte <<= 1;
            }
        }
        if (t == 0)
        {
            temp = dat & 0xFF;
        }
    }
    if (crc_byte == crc_data)
    {
        temp = 0;
    }
    else
    {
        temp = 1;
    }
    return temp;
}

/**************************************
��ʼ�ź�
**************************************/
void SHTC3_WriteCmd(unsigned short cmd)
{
    SHTC3_IIC_Start();
    SHTC3_IIC_SendByte(SHTC3_DEFAULT_ADDR);
    SHTC3_IIC_SendByte(cmd >> 8);
    SHTC3_IIC_SendByte(cmd);
    SHTC3_IIC_Stop();
}

void SHTC3_Reset()
{
    SHTC3_WriteCmd(SHTC3_SOFTRESET);
}

void SHTC3_Wakeup()
{
    SHTC3_WriteCmd(SHTC3_WAKEUP);
    SHTC3_IIC_Stop();
    delay_ms(1);
}
void SHTC3_Sleep()
{
    SHTC3_WriteCmd(SHTC3_SLEEP_CMD);
    SHTC3_IIC_Stop();
}

unsigned short SHTC3_ReadId()
{
    unsigned char buf[2];
    unsigned short res;

    SHTC3_IIC_Start();
    SHTC3_IIC_SendByte(SHTC3_DEFAULT_ADDR);
    SHTC3_IIC_SendByte(SHTC3_READID >> 8);
    SHTC3_IIC_SendByte(SHTC3_READID);
    SHTC3_IIC_Start();
    SHTC3_IIC_SendByte(SHTC3_DEFAULT_ADDR | 0x01); // ������

    buf[0] = IIC_RecvByte(0);
    // STHC3_IIC_SendACK(0);  // ��ӦACK
    buf[1] = IIC_RecvByte(1);
    // STHC3_IIC_SendACK(1);  // ��ӦACK

    SHTC3_IIC_Stop();

    res = (buf[0] << 8) | buf[1];

    res = (res & 0x083F);
    printf("SHTC3 ID: 0x%04x\r\n", res);
    return res;
}
void SHTC3_ReadTemp()
{
    u8 Cnt;
    u8 Temp_Check_Num;
    u8 Hum_Check_Num;
    u32 TEMP_MEASUREMENT;
    u32 HUM_MEASUREMENT;
    u32 result;

    SHTC3_Wakeup();
    SHTC3_WriteCmd(SHTC3_NOR_READ_TEMP_FIR_DIS_CMD);
    delay_ms(20);

    SHTC3_IIC_Start();
    SHTC3_IIC_SendByte(SHTC3_DEFAULT_ADDR | 0x01); // ��

    TEMP_MEASUREMENT = IIC_RecvByte(0);
    TEMP_MEASUREMENT <<= 8;
    TEMP_MEASUREMENT |= IIC_RecvByte(0);
    Temp_Check_Num = IIC_RecvByte(0);

    HUM_MEASUREMENT = IIC_RecvByte(0);
    HUM_MEASUREMENT <<= 8;
    HUM_MEASUREMENT |= IIC_RecvByte(0);
    Hum_Check_Num = IIC_RecvByte(1);

    SHTC3_IIC_Stop();

    Cnt = SHTC3_CRC_CHECK(TEMP_MEASUREMENT, Temp_Check_Num);

    if (Cnt == 0)
    {
        TEMP_MEASUREMENT = TEMP_MEASUREMENT * 1750 / 65535;
        if(TEMP_MEASUREMENT>=450)
        {
           TEMP_DATA_Cont = 1;
            TEMP_MEASUREMENT = TEMP_MEASUREMENT -450;
            tempt_dath =  TEMP_MEASUREMENT/10;
            tempt_datl = TEMP_MEASUREMENT%10;
            // printf("tem: %bd | %bd \r\n",tempt_dath,tempt_datl );
            sprintf(TEMP_DATA, "%bd.%bd", tempt_dath,tempt_datl);
            // printf("TEM: %s\r\n", TEMP_DATA );
        }
        else
        {
            TEMP_DATA_Cont = 0;
            TEMP_MEASUREMENT = 450 - TEMP_MEASUREMENT;
            tempt_dath =  TEMP_MEASUREMENT/10;
            tempt_datl = TEMP_MEASUREMENT%10;
			sprintf(TEMP_DATA, "%bd.%bd", tempt_dath,tempt_datl);
        }
        // printf("SHTC3 TEMP: %ld\r\n", TEMP_MEASUREMENT);
    }

    Cnt = SHTC3_CRC_CHECK(HUM_MEASUREMENT, Hum_Check_Num);

    if (Cnt == 0)
    {
        result = HUM_MEASUREMENT * 100;
        HUM_MEASUREMENT = (u8)(result >> 16);
        humi_dataa = HUM_MEASUREMENT;
        sprintf(HUMI_DATA, "%bd", humi_dataa);
        // printf("SHTC3 HUM: %ld\r\n", HUM_MEASUREMENT);
        // HUMI_DATA = HUM_MEASUREMENT;
    }
    SHTC3_Sleep();
}
void SHTC3_Init(void)
{
    SHTC3_IO_config();
    SHTC3_Wakeup();
    SHTC3_Reset();
    delay_ms(10);
    SHTC3_ReadId();
}